package com.yihang.springboot_yihang.exception;

import com.yihang.springboot_yihang.pojo.ResponseMessage;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.View;

@RestControllerAdvice //统一处理
public class GlobalExceptionHandlerAdvice {


    Logger log = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);


    @ExceptionHandler({Exception.class})
    public ResponseMessage<?> handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {


        //记录日志
        log.error("统一异常",e);

        return new ResponseMessage<>()
                .setCode(500)
                .setMessage("error")
                .setData(null);


    }
}
